﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Commonlib;
namespace AlbonWeb.Manager.SizeManager
{
    public partial class TempleteDesign : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定 style item
                string cmdstr = "select * from Style where parentID=0 and active=1";
                SqlHelper.BindDropDownList(ddlItem, "styleName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                //绑定size item
                bindRep();
                if (Request["styleid"] != null)
                {
                    ddlItem.SelectedValue = Request["styleid"].ToString();
                    initData();
                }
            }
        }
        protected void bindRep()
        {
            string cmdstr = "select * from SizeItem where active=1";
            SqlHelper.BindRepeater(repMain,SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null);
        }
        //改变所选款式
        protected void ddlItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            initData();
        }

        //初始化数据
        protected void initData()
        {
            string cmdstr = "select * from SizeTempleteView where styleID=" + ddlItem.SelectedValue;
            DataTable dt = SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).Tables[0];
            string value = "";
            foreach(DataRow dr in dt.Rows)
            {
                value +="$"+ dr["sizeItemID"].ToString() + "," + dr["bodyStatus"].ToString() + "," + dr["finishedStatus"].ToString() ;
            }
            if (value.IndexOf('$') == 0)
            {
                value = value.Substring(1, value.Length - 1);
            }
            this.ClientScript.RegisterStartupScript(Page.GetType(), "initTemplete", "initTemplete('" + value + "')", true);
        }

        //提交所订制的尺寸模板信息
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string value = hfvalue.Value;
            string[] templetevalue = value.Split('$');
            Albon.Model.SizeTemplete templete = new Albon.Model.SizeTemplete();
            templete.styleID =int.Parse(ddlItem.SelectedValue);
            BLL.SizeTempleteBLL sizetempletebll = new BLL.SizeTempleteBLL();
            sizetempletebll.deleteTemplete(templete.styleID);
            foreach (string str in templetevalue)
            {
                string[] list = str.Split(',');
                
                templete.ItemID =int.Parse(list[0]);
                templete.bodyStatus = int.Parse(list[1]);
                templete.finishedStatus = int.Parse(list[2]);
               
                sizetempletebll.addTemplete(templete);
                
            }
            JavaScriptHelper.CloseWindow();
        }

        
    }
}